<?php
/*
 MAIL.INC.PHP by martin $ 2009/02/19 01:49:56
*/

function send_jp_mail($subj='', $body='', $file='', $address=''){
 global $mb, $ip, $mode;
 if(strpos($_SERVER['REMOTE_ADDR'], '0.0.1')) return;
 $sep = md5(time());
 $pat = "{^[0-9A-Za-z\._\-';\s#\*/&\+!]+$}";
 $subj = str_replace('`', NL, sanitize_data($subj));
 $body = str_replace('`', NL, sanitize_data($body));
 
 if(preg_match($pat, BLOG_NAME)){
  $from = BLOG_NAME; // Thanks, naoK!
 } else {
  $from = '=?ISO-2022-JP?B?'.base64_encode(my_convert_encoding(BLOG_NAME, 'JIS', ENCODE)).'?=';
 }
 $from = str_replace(array('&#039;'), array("'"), $from).' <info@'.str_replace('http://', '', dirname(ROOT_PATH)).'>';
 $subj = '=?ISO-2022-JP?B?'.base64_encode(my_convert_encoding($subj, 'JIS', ENCODE)).'?=';
 $body = preg_replace('{\[g\]([^\/]+?)\[/g\]}', '$1', $body);
 $body = preg_replace('{\[link:([;/?@&=\+\$,\w\-\.!~%#\|]+)(\])(.*?)\[/link\]}i','<a href="http://$1">$3</a>', $body);
 $body = my_convert_encoding(str_replace(array('&#96;','&amp;','&#44','&#039;','&#37;'), array(NL,'&',',',"'",'%'), $body), 'JIS', ENCODE);

 if($file!=''){
  $header =<<<_HEAD
Mine-Version: 1.0
Content-Type: multipart/mixed;
 boundary="$sep";
From: $from

_HEAD;
  $encode = chunk_split(base64_encode(get_file_content($file)));
  $body =<<<_BODY
--$sep
Content-Type: text/plain; charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit

$body

--$sep
Content-Type: text/plain; name="$file"
Content-Transfer-Encoding: base64

$encode

--$sep--

_BODY;
 } else {
  $header =<<<_HEAD
From: $from
Content-Type: text/plain; charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit
_HEAD;
 }
 if(preg_match("{^[^@]+@[^.]+\..+}", $address)){
  @mail($address, $subj, $body, $header); // $address が指定されていればそこに送信
 } else {
  $mail_adrs = explode(',', MAILTO);
  foreach($mail_adrs as $m) @mail(trim($m), $subj, $body, $header);
 }
}

?>